<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>代理管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			loadSheng();
		});
		function page(n,s){
			$("#pageNo").val(n);
			$("#pageSize").val(s);
			$("#searchForm").submit();
        	return false;
        }
		
		//加载省
		function loadSheng(){
			var shengId = '${institution.province}';
			var quanName = '';
			var url = "";
			var parameter = {};
			//服务预约列表信息
			url = "${ctx}/c/getSheng";
			$.ajax({
				 type: "post",
				 url: url,
				 dataType : 'json',
				 data: parameter,
				 success: function(data, textStatus){
					 var data = data.data;
					 var html = "";
					 var m = -1;
					 for(var i = 0 ; i < data.length ; i++ ){
						 var p = data[i];
						 if(shengId == p.id){
							 quanName = p.name;
							 m = i;
							 html = html + '<option value="'+p.id+'" selected="selected" >'+p.name+'</option>'; 
						 }else
						 	html = html + '<option value="'+p.id+'" >'+p.name+'</option>'; 
					 }
					 $("#province").html(html);
					 $("#province").get(0).selectedIndex = m;
					 $("div.select2-container").each(function(){
						 	var p = $(this).context;
						 	var pid = p.id;
						 	if(pid.indexOf('province') > 0){
						 		p.firstElementChild.firstElementChild.innerText = quanName;
						 	}
					 });
					 
					  outputCity();
				 },
				 complete: function(XMLHttpRequest, textStatus){
				 },
				 error: function(){
				 }
			});
		};
		
		//加载市
		function loadCity(officeId){
			var cityId = '${institution.city}';
			var quanName = '';
			var url = "";
			var parameter = {};
			//服务预约列表信息
			url = "${ctx}/c/getCity";
			parameter.parentId = officeId;//$('#officeIdId').val();
			$.ajax({
				 type: "post",
				 url: url,
				 dataType : 'json',
				 data: parameter,
				 success: function(data, textStatus){
					 var data = data.data;
					 var html = "";
					 var m = -1;
					 for(var i = 0 ; i < data.length ; i++ ){
						 var p = data[i];
						 if(cityId == p.id){
							 quanName = p.name;
							 m = i;
							 html = html + '<option value="'+p.id+'" selected="selected" >'+p.name+'</option>'; 
						 }else
						 	html = html + '<option value="'+p.id+'" >'+p.name+'</option>'; 
					 }
					 $("#city").html(html);
					 $("#city").get(0).selectedIndex = m;
					 $("div.select2-container").each(function(){
						 	var p = $(this).context;
						 	var pid = p.id;
						 	if(pid.indexOf('city') > 0){
						 		p.firstElementChild.firstElementChild.innerText = quanName;
						 	}
					 });
					  outputCouny();
				 },
				 complete: function(XMLHttpRequest, textStatus){
				 },
				 error: function(){
				 }
			});
		};
		
		//加载县/区
		function loadCountry(officeId){
			var countyId = '${institution.county}';
			var quanName = '';
			var url = "";
			var parameter = {};
				//服务预约列表信息
			url = "${ctx}/c/getCountry";
			parameter.parentId = officeId;//
			$.ajax({
				 type: "post",
				 url: url,
				 dataType : 'json',
				 data: parameter,
				 success: function(data, textStatus){
					 var data = data.data;
					 var html = "";
					 var m = -1;
					 for(var i = 0 ; i < data.length ; i++ ){
						 var p = data[i];
						 if(countyId == p.id){
							 quanName = p.name;
							 m = i;
							 html = html + '<option value="'+p.id+'" selected="selected" >'+p.name+'</option>'; 
						 }else
						 	html = html + '<option value="'+p.id+'" >'+p.name+'</option>'; 
					 }
					 $("#county").html(html);
					 $("#county").get(0).selectedIndex = m;
					 $("div.select2-container").each(function(){
						 	var p = $(this).context;
						 	var pid = p.id;
						 	if(pid.indexOf('county') > 0){
						 		p.firstElementChild.firstElementChild.innerText = quanName;
						 	}
					 });
				 },
				 complete: function(XMLHttpRequest, textStatus){
				 },
				 error: function(){
				 }
			});
		};
		
		function outputCity(){
			loadCity($("#province").val());
		}
		
		function outputCouny(){
			loadCountry($("#city").val());
		}
		
		//初始化数据
		function savetTb(userId){
			//服务预约列表信息
			url = "${ctx}/sys/agent/savetTb/id/"+userId;
			$.ajax({
				 type: "get",
				 url: url,
				 dataType : 'json',
				// data: parameter,
				 success: function(data, textStatus){
					alert("初始化成功");
				 },
				 complete: function(XMLHttpRequest, textStatus){
				 },
				 error: function(){
				 }
			});
		};
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/sys/agent/">代理列表</a></li>
		<li><a href="${ctx}/sys/agent/form">代理添加</a></li>
	</ul>
	<form:form id="searchForm" modelAttribute="institution" action="${ctx}/sys/agent/" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<ul class="ul-form">
			<li><label>机构名称：</label>
				<form:input path="institutionName" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li><label>负责人：</label>
				<form:input path="userName" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li><label>联系电话：</label>
				<form:input path="userPhone" htmlEscape="false" maxlength="64" class="input-medium"/>
			</li>
			<li><label>省：</label>
				<form:select path="province" class="input-xlarge " onchange="outputCity()">
					<form:options itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
			</li>
			<li><label>市：</label>
				<form:select path="city" class="input-xlarge " onchange="outputCouny()">
					<form:options  itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
			</li>
			<li><label>县：</label>
				<form:select path="county" class="input-xlarge " >
					<form:options  itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
			</li>
			<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/></li>
			<li class="clearfix"></li>
		</ul>
	</form:form>
	<sys:message content="${message}"/>
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
				<th>UUID</th>
				<th>机构名称</th>
				<th>负责人</th>
				<th>手机号</th>
				<th>区域</th>
				<th>地址</th>
				<th>注册时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${page.list}" var="institution">
			<tr>
				<td>
				<a href="${ctx}/sys/agent/form?id=${institution.id}">
					${institution.id}
				</a>
				</td>
				<td>
					${institution.institutionName}
				</td>
				<td>
					${institution.userName}
				</td>
				<td>
					${institution.userPhone}
				</td>
				<td>
					${institution.shengName}${institution.cityName}${institution.xianName}
				</td>
				<td>
					${institution.address}
				</td>
				<td>
					<fmt:formatDate value="${institution.createDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
				</td>
				<td>
    				<a href="${ctx}/sys/agent/form?id=${institution.id}">修改</a>
					<a href="${ctx}/sys/agent/delete?id=${institution.id}" onclick="return confirmx('确认要删除该代理吗？', this.href)">删除</a>
					<a href="#" onclick="savetTb(${institution.id})">初始化数据</a>
				</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${page}</div>
</body>
</html>